﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class CreateReservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               
                //ArrayList roomIdList = BookingAndRoomAvailabilityDBManager.GetAllRoomID();
                //ddlRoomNo.DataSource = roomIdList;
                //ddlRoomNo.DataBind();
                //ddlRoomNo.SelectedValue = "View All";

                string status = "Vacant";
                ArrayList vacantRoomList = BookingAndRoomAvailabilityDBManager.GetAllVacantRoomID(status);
                ddlRoomNo.DataSource = vacantRoomList;
                ddlRoomNo.DataBind();
                ddlRoomNo.SelectedValue = "View All";
                

           
            }
            string desiredArrivalDate = Request.QueryString["desiredArrivalDate"];
            string arrivalTime = Request.QueryString["arrivalTime"];
            string desiredDepartDate = Request.QueryString["desiredDepartDate"];
            string departTime = Request.QueryString["departTime"];
            string numOfNight = Request.QueryString["numOfNight"];
            string numOfAdult = Request.QueryString["numOfAdult"];
            string numOfChildren = Request.QueryString["numOfChildren"];
            string bedType = Request.QueryString["bedType"];
            string area = Request.QueryString["area"];
            string remarks = Request.QueryString["remarks"];
            string location = Request.QueryString["location"];

            lblCheckInDate.Text = desiredArrivalDate;
            lblCheckInTime.Text = arrivalTime;
            lblCheckOutDate.Text = desiredDepartDate;
            lblCheckOutTime.Text = departTime;

            tbxNumOfNight.Text = numOfNight;
            tbxNumOfAdult.Text = numOfAdult;
            tbxNumOfChildren.Text = numOfChildren;

            lblBedType.Text = bedType;
            tbxArea.Text = area;
            tbxRemarks.Text = remarks;
            lblLocation.Text = location;

        }

        protected void btnSearchExistingID_Click(object sender, EventArgs e)
        {
            int gId = Convert.ToInt32(tbxGuestID.Text);

            if (BookingAndRoomAvailabilityDBManager.CheckExistingGuestId(gId) == true)
            {
                lblExistingGuestId.Text = "Guest ID already exists.";
            }
            else
            {
                lblExistingGuestId.Text = "Please proceed to fill in other booking related information.";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string arrivalDate = ddlArrivalDay.SelectedValue + "/" + ddlArrivalMonth.SelectedValue + "/" + tbxArrivalYear.Text;
            DateTime dt_arrivalDate = DateTime.Parse(arrivalDate);
             
            string arrivalTime = tbxArrivalTime.Text;

            string departDate = ddlDepartDay.SelectedValue + "/" + ddlDepartMonth.SelectedValue + "/" + tbxDepartYear.Text;
            DateTime dt_departDate = DateTime.Parse(departDate);

            string departTime = tbxDepartTime.Text;

            int numOfNight = Convert.ToInt32(tbxNumOfNight.Text);
            int numOfAdult = Convert.ToInt32(tbxNumOfAdult.Text);
            int numOfChildren = Convert.ToInt32(tbxNumOfChildren.Text);

            string roomNum = ddlRoomNo.SelectedValue;

            string remarks = tbxRemarks.Text;
            string area = tbxArea.Text;
            string paymentOption = rbPaymentOption.SelectedValue;
            int guestId = Convert.ToInt32(tbxGuestID.Text);

            string title = ddlTitle.SelectedValue;
            string firstName = tbxFirstName.Text;
            string lastName = tbxLastName.Text;
            string gender = rbGender.SelectedValue;

            string dob = ddlDobDay.SelectedValue + "/" + ddlDobMonth.SelectedValue + "/" + tbxDobYear.Text;
            DateTime dt_dob = DateTime.Parse(dob);

            string email = tbxEmail.Text;
            int mobileNum = Convert.ToInt32(tbxMobileNum.Text);

            int homeNum = Convert.ToInt32(tbxHomeNum.Text);

            string address = tbxAddress.Text;
            string city = tbxCity.Text;
            string state = tbxState.Text;
            int zipCode = Convert.ToInt32(tbxZip.Text);
            string country = tbxCountry.Text;

            string bedType = lblBedType.Text;
            string location = lblLocation.Text;

            string cardHolderName = tbxCardHolderName.Text;
            string type = ddlType.SelectedValue;
            string cardNum = tbxCardNum.Text;
            string expiryDate = ddlExpiryDay.SelectedValue + "/" + ddlExpiryMonth.SelectedValue + "/" + tbxExpiryYear.Text;
            DateTime dt_expiryDate = DateTime.Parse(expiryDate);

            Guest newGuest=new Guest(guestId, title, firstName, lastName, gender, email, mobileNum, homeNum,address, city, state, zipCode,country,dt_dob);

            BookingAndRoomAvailabilityDBManager.InsertGuestInfo(newGuest);

            CreditCard newCreditCard= new CreditCard(cardNum, cardHolderName, type, dt_expiryDate, guestId);

            BookingAndRoomAvailabilityDBManager.InsertCreditCardInfo(newCreditCard);

          BookingInfo newBooking = new BookingInfo(dt_arrivalDate,arrivalTime,dt_departDate,departTime,numOfNight,numOfAdult,numOfChildren,roomNum,paymentOption, remarks,area, bedType,location,guestId);

         
          if (BookingAndRoomAvailabilityDBManager.InsertBookingInfo(newBooking) == 1)
          {
              Server.Transfer("CheckInGuestSaved.aspx");
          }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("BookingPreferenceOfRoom.aspx");
        }


        
    }
}